package com.islewars.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.islewars.client.dto.PlayerDto;
import com.islewars.client.lobby.Lobby;
import com.islewars.client.service.GameService;
import com.islewars.client.service.GameServiceAsync;

public class LoginForm extends Composite {
	private static GameServiceAsync gameServiceAsync = GWT.create(GameService.class);

	Panel panel;

	private Label lblError = new Label("");

	public LoginForm() {
		panel = new VerticalPanel();
		panel.setWidth("100%");
		panel.setHeight(Window.getClientHeight() + "px");
		((VerticalPanel)panel).setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		((VerticalPanel)panel).setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
	    FlexTable flexTable = new FlexTable();
	    //flexTable.setBorderWidth(2);
	    flexTable.setCellSpacing(6);
	    FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();


	    // Add some standard form options
	    flexTable.setHTML(1, 0, "<b>Player:</b>");
	    final TextBox usernameTB = new TextBox();
	    usernameTB.setText("Guest");
	    flexTable.setWidget(1, 1, usernameTB);
//	    flexTable.setHTML(2, 0, "<b>Password:</b>");
//	    final PasswordTextBox passwordTB = new PasswordTextBox();
//	    passwordTB.setText("wrk478usr");
//	    flexTable.setWidget(2, 1, passwordTB);
	    
		Button btnLogin = new Button("Login");
		flexTable.setWidget(3, 0, btnLogin);
	    cellFormatter.setColSpan(3, 0, 2);

	    cellFormatter.setHorizontalAlignment(
		        3, 0, HasHorizontalAlignment.ALIGN_CENTER);

		flexTable.setWidget(4, 0, lblError);
	    cellFormatter.setColSpan(4, 0, 2);
	    cellFormatter.setHorizontalAlignment(
		        4, 0, HasHorizontalAlignment.ALIGN_CENTER);

		/**
		 * Add a click listener which is called when the button is clicked
		 */
		btnLogin.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				login(usernameTB.getText());
			}
		});
		
		usernameTB.setFocus(true);
		
		panel.add(flexTable);	
		panel.setVisible(true);
	}

	public Panel getPanel() {
		return panel;
	}
	
	/**
	 * This method is called when the button is clicked
	 */
	private void login(final String userName) {
		GWT.log("Checking login for: " + userName);
		
		gameServiceAsync.login(userName, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				GWT.log("onSuccess: " + result);

				ClientStaticData.player = new PlayerDto(userName);
				new Lobby();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
		});
	}

	private void setErrorText(String errorMessage) {
		lblError.setText(errorMessage);
	}
}
